package com.atguigu.gmall.user.service.impl;

import com.atguigu.gmall.model.user.UserAddress;
import com.atguigu.gmall.model.user.UserInfo;
import com.atguigu.gmall.user.mapper.UserAddressMapper;
import com.atguigu.gmall.user.mapper.UserInfoMapper;
import com.atguigu.gmall.user.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

/**
 * @author mgx
 * @version 1.0
 * @description: TODO
 * @date 2022-12-01 13:32
 */
@Service
@SuppressWarnings("all")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserAddressMapper userAddressMapper;


    /**
     * 用户登录 -- 返回用户信息
     * @param userInfo
     * @return
     */
    @Override
    public UserInfo login(UserInfo userInfo) {
        // sql语句相当于是 select * from user_info where username = ? and passwd = ?
        // 获取密码
        String passwd = userInfo.getPasswd();
        // 对密码进行加密
        String newPasswd = DigestUtils.md5DigestAsHex(passwd.getBytes());
        // 获取用户名
        String loginName = userInfo.getLoginName();
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("login_name", loginName);
        queryWrapper.eq("passwd", newPasswd);
        // 查询
        UserInfo user = userInfoMapper.selectOne(queryWrapper);
        if(user != null) {
            return user; // 返回用户信息
        }
        return null;
    }

    /**
     * 查询用户地址列表
     * @param userId
     * @return
     */
    @Override
    public List<UserAddress> findUserAddressListByUserId(Long userId) {
        // select * from user_address where user_id = userId
        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<UserAddress> userAddressList = userAddressMapper.selectList(queryWrapper);
        return userAddressList;
    }
}
